package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.TbOwner;
import com.ruoyi.system.mapper.TbOwnerTypeMapper;
import com.ruoyi.system.domain.TbOwnerType;
import com.ruoyi.system.service.ITbOwnerTypeService;

/**
 * 房东类型Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-11
 */
@Service
public class TbOwnerTypeServiceImpl implements ITbOwnerTypeService 
{
    @Autowired
    private TbOwnerTypeMapper tbOwnerTypeMapper;

    /**
     * 查询房东类型
     * 
     * @param id 房东类型主键
     * @return 房东类型
     */
    @Override
    public TbOwnerType selectTbOwnerTypeById(Long id)
    {
        return tbOwnerTypeMapper.selectTbOwnerTypeById(id);
    }

    /**
     * 查询房东类型列表
     * 
     * @param tbOwnerType 房东类型
     * @return 房东类型
     */
    @Override
    public List<TbOwnerType> selectTbOwnerTypeList(TbOwnerType tbOwnerType)
    {
        return tbOwnerTypeMapper.selectTbOwnerTypeList(tbOwnerType);
    }

    /**
     * 新增房东类型
     * 
     * @param tbOwnerType 房东类型
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbOwnerType(TbOwnerType tbOwnerType)
    {
        // 只保留icon字段的第一个url
        if (tbOwnerType.getIcon() != null && tbOwnerType.getIcon().contains(",")) {
            tbOwnerType.setIcon(tbOwnerType.getIcon().split(",")[0]);
        }
        int rows = tbOwnerTypeMapper.insertTbOwnerType(tbOwnerType);
        insertTbOwner(tbOwnerType);
        return rows;
    }

    /**
     * 修改房东类型
     * 
     * @param tbOwnerType 房东类型
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbOwnerType(TbOwnerType tbOwnerType)
    {
        // 只保留icon字段的第一个url
        if (tbOwnerType.getIcon() != null && tbOwnerType.getIcon().contains(",")) {
            tbOwnerType.setIcon(tbOwnerType.getIcon().split(",")[0]);
        }
        tbOwnerTypeMapper.deleteTbOwnerByTypeId(tbOwnerType.getId());
        insertTbOwner(tbOwnerType);
        return tbOwnerTypeMapper.updateTbOwnerType(tbOwnerType);
    }

    /**
     * 批量删除房东类型
     * 
     * @param ids 需要删除的房东类型主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbOwnerTypeByIds(Long[] ids)
    {
        tbOwnerTypeMapper.deleteTbOwnerByTypeIds(ids);
        return tbOwnerTypeMapper.deleteTbOwnerTypeByIds(ids);
    }

    /**
     * 删除房东类型信息
     * 
     * @param id 房东类型主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbOwnerTypeById(Long id)
    {
        tbOwnerTypeMapper.deleteTbOwnerByTypeId(id);
        return tbOwnerTypeMapper.deleteTbOwnerTypeById(id);
    }

    /**
     * 新增房东管理信息
     * 
     * @param tbOwnerType 房东类型对象
     */
    public void insertTbOwner(TbOwnerType tbOwnerType)
    {
        List<TbOwner> tbOwnerList = tbOwnerType.getTbOwnerList();
        Long id = tbOwnerType.getId();
        if (StringUtils.isNotNull(tbOwnerList))
        {
            List<TbOwner> list = new ArrayList<TbOwner>();
            for (TbOwner tbOwner : tbOwnerList)
            {
                tbOwner.setTypeId(id);
                list.add(tbOwner);
            }
            if (list.size() > 0)
            {
                tbOwnerTypeMapper.batchTbOwner(list);
            }
        }
    }
}
